package com.eop.managedBean;

import java.util.List;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.attachment.entity.Attachment;
import com.base.attachment.service.AttachmentException;
import com.base.attachment.service.impl.AttachmentService;
import com.base.commons.SpringBeanHolder;

@ManagedBean(name = "eop_listFlowBean", scope = ManagedBeanScope.REQUEST)
public class ListFlowBean {
	private AttachmentService attachmentServcie = SpringBeanHolder
			.getBean("attachmentSrv");
	@Bind(id = "attachmentsList", attribute = "binding")
	private UIDataGrid attachmentsList_binding;
	@SaveState
	@Accessible
	private String beanClass = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("beanClass");
	@SaveState
	@Accessible
	private String beanId = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("beanId");
	@SaveState
	@Accessible
	private String attachmentType = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap()
			.get("attachmentType");
	@Inject
	private FacesContext context;

	@DataModel(id = "attachmentsList")
	public List<Attachment> getAll() {
		try {
			return attachmentServcie.getAttachmentsByBeanAndType(beanClass,
					beanId, attachmentType);
		} catch (AttachmentException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Action(id = "delProcess")
	public void delProcess() {
		Object[] objs = attachmentsList_binding.getSelectedValues();
		try {
			for (Object att : objs) {
				attachmentServcie.removeAttachment((Attachment) att);
			}
		} catch (AttachmentException e) {
			e.printStackTrace();
		}
		attachmentsList_binding.reload();

	}

	@Action(id = "executeProcess")
	public void executeProcess() {
		Object[] obj = attachmentsList_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		Attachment att = (Attachment) obj[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/process/design/executeImage.jsp?id=" + att.getId();
		
		String script = "javascript:window.open('" + url
				+ "')";
		Browser.execClientScript("window.top", script);
		// Browser.execClientScript("javascript:exeProcess('" + url + "');");

	}

	@Action(id = "editProcess")
	public void editProcess() {
		Object[] obj = attachmentsList_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		Attachment att = (Attachment) obj[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/process/design/drawProcess.jsp?id=" + att.getId();

		String script = "addCenterLayoutTab('drawImage','编辑流程','" + url + "')";
		Browser.execClientScript("window.top", script);
	}
}
